// index.js
// 获取应用实例
const app = getApp()
// const request = require('../../utils/request')
Page({
  data: {
    count: [],
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    banners: [],
    new: [],
    newsIndex: 0,
    newsTimer:0,
    newsItems: 'transform: translate(0rpx,0rpx):transiton:transform 0.3s;',
    lists:[],
    pageNum:1,
    total:0,
    loading:'数据已加载中'
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
    
  },
  getbanners() {
    return new Promise((resolve,reject) =>{
       wx.request({
      url: 'https://obuge.com/juzao/index/banners',//轮播图接口
      success: (res) => {
          this.setData({
            banners: res.data
          })
          resolve()
      }
    })
  })
  },
  getCategorys(callback) {
    return new Promise((resolve,reject) =>{
       wx.request({
      url: 'https://obuge.com/juzao/index/categorys',//轮播图接口
      success: (res) => {
          this.setData({
            categorys: res.data
          })
          resolve()
      }
    })
    })
  },
  getnews() {
    return new Promise((resolve,reject) =>{
      wx.$https.get('/index/news').then((res) =>{
          this.setData({
            news: res.data
          })
         resolve()
      }).then(() => {
        this.changeNewsIndex()//触发新闻轮播
      })
      resolve()
    })
   wx.$https.get('/index/news').then((res) =>{
    return new Promise((resolve,reject) =>{
      this.setData({
        news: res.data
      })
     resolve()
    })
  }).then(() => {
    this.changeNewsIndex()//触发新闻轮播
    if(typeof callback== 'function') callback()
  })
  },
onReachBottom(){
  if(this.data.total > this.data.lists.length){
this.data.pageNum ++
    this.getRecomend()
  }
    
  },
  getRecomend(){
    return new Promise((resolve,reject)=>{
          this.setData({loading:'数据加载中...'})
    wx.request({
      url: 'https://obuge.com/juzao/index/recommends?pageNum=' + this.data.pageNum,//分页接口
      success: (res) => {
          this.setData({
            loading:'已经到底了哦~',
            lists:this.data.lists.concat(res.data.rows),
            total:res.data.total
          })
          resolve()
      }
    })
    })


  },
  changeNewsIndex() { //新闻轮播
    if(this.data.newsTimer){
      clearInterval(this.data.newsTimer)
      this.data.newsTimer = 0
    }
    this.data.newsTimer = setInterval(() => {
      let newsIndex = this.data.newsIndex
      newsIndex++
      if (newsIndex == this.data.news.length) newsIndex = 0//
      this.setData({
        newsIndex,
        newItems:'transform:translate(0rpx, -' +newsIndex * 80 + 'rpx);transition:transform 0.3s;'
       })
    }, 3000)
  },
  endaimation(){
    //完成动画
    if(this.data.newsIndex == 4){
      this.setData({
        newsIndex:0,
        newItems:'transform: translate(0rpx,0rpx);'
      })
    }
  },
//下拉刷新
onPullDownRefresh(){ 
  this.data.pageNum = 1
  this.data.lists = []
  Promise.all([this.getbanners(),this.getnews(),this.getRecomend(), this.getCategorys()]).then(()=>{
    wx.stopPullDownRefresh()
}).catch(e =>[
  wx.stopPullDownRefresh()
])
},

  onLoad() {
    Promise.all([this.getbanners(),this.getnews(),this.getRecomend(), this.getCategorys()]).then(()=>{
    }).catch(e =>[
      console.log(e)
    ])
  },

  
  onShow() {
   
  },
  onReady() {
    
  },
  tapNamebig(even) {
   
    let count = this.data.count
    count++
    this.setData({
      count: count
    })
  },
  tapName(event) {
    console.log('点击成功', event)
    wx.showModal({
      cancelColor: 'cancelColor',
      title: '提示',
      content: event.currentTarget.dataset.path,
      success(e) {
        console.log(event.currentTarget.dataset.path)
        wx.switchTab({
          url: '/pages/logs/logs'
        })
      }
    })
  },
  newgoods(even){
  
    let count = this.data.count
    count++
    this.setData({
      count: count
    })
  },

  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
